package com.ming.controller;

import com.ming.dao.StaffMapper;
import com.ming.dao.VehicleMapper;
import com.ming.pojo.Staff;
import com.ming.pojo.Vehicle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @author Xu
 * Created by Intellij IDEA.
 * @create 2021/4/15 10:07
 */
@Controller
public class VehicleController {
    @Autowired
    VehicleMapper vehicleMapper;

    @RequestMapping("/vehicles")
    public String list(Model model){
        List<Vehicle> vehicles = vehicleMapper.queryVehicles();
        model.addAttribute("vehicles",vehicles);
        return "vehicles/list";
    }

    @GetMapping("/vehicle")
    public String toAddPage(Model model){
        //查出所有部门信息
        List<Vehicle> vehicles = vehicleMapper.queryVehicles();
        model.addAttribute("vehicles",vehicles);
        return "vehicles/add";
    }

    @PostMapping("/vehicle")
    public String addVehicle(Vehicle vehicle){
        Integer mId = vehicleMapper.getMId();
        vehicle.setVid(mId);
        vehicleMapper.addVehicle(vehicle);
        return "redirect:/vehicles";
    }

    //去客户的修改页面
    @GetMapping("/vehicle/{id}")
    public String toUpdateVehicle(@PathVariable("id") int id, Model model) {
        //查出原来的数据
        Vehicle vehicle = vehicleMapper.queryVehicleById(id);
        model.addAttribute("vehicle", vehicle);
        return "vehicles/update";
    }

    @PostMapping("/updateVehicle")
    public String updateVehicle(Vehicle vehicle) {
        vehicleMapper.updateVehicle(vehicle);
        return "redirect:/vehicles";
    }


    @GetMapping("/deleteVehicle/{id}")
    public String deleteVehicle(@PathVariable("id")Integer id){
        vehicleMapper.deleteVehicle(id);
        return "redirect:/vehicles";
    }

}
